//
//  LJM_HotSongs_TableViewCell.m
//  wind
//
//  Created by 李金明 on 16/1/19.
//  Copyright © 2016年 李金明. All rights reserved.
//

#import "LJM_HotSongs_TableViewCell.h"
#import "LJM_HotSongs_CollectionViewCell.h"
#import "LJM_HotSong_Model.h"
//歌单网址
#define SONGURL @"http://search.dongting.com/songlist/search?q=tag:最热&page=1&size=10&app=ttpod&v=v8.1.3.2015112716&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=7.0.4&active=1&net=2&openudid=1389d9dc18cb741587d90a5aead8646421f95dc0&idfa=C9BC818C-A0F5-4F04-8F8D-841910E08DF7&utdid=VmGiKEXHDQ0DAIL2FOnFFzxV&alf=201200&bundle_id=com.ttpod.music&latitude=&longtitude="
//collectionfowl的坐标
#define COLLECTIONFOWLEDGE 5//边界
#define COLLECTWIDTH 20
#define COLLECTHEADHEIGHT 20//区头的高度
#define HEADHEIGHT 20//头的高度
#define TOPHEIGHT 30//tableView的头视图的高度

#define TITLELEFT 10//标题距离左侧的距离
#define TITLEWIDTH 80//标题的宽度
#define MOVEWIDTH 60//更多的宽度
@interface LJM_HotSongs_TableViewCell () <UICollectionViewDataSource,  UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) UIButton *moreBut;
@property (nonatomic, retain) UILabel *titleLab;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLy;
@end

@implementation LJM_HotSongs_TableViewCell
- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr) {
        [_dataArr release];
        _dataArr = [dataArr retain];
    }
    [self.collectionV reloadData];
}
- (void)dealloc
{
    self.delegate = nil;
    [_dataArr release];
    [_flowLy release];
    [_titleLab release];
    [_moreBut release];
    [_collectionV release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatView];
    }
    return self;

}
- (void)creatView
{
    self.backgroundColor = [UIColor whiteColor];
    
    [self addHeadView];
    [self addCollectionView];
}
- (void)layoutSubviews
{
    self.flowLy.sectionInset = UIEdgeInsetsMake(COLLECTIONFOWLEDGE + COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE, COLLECTIONFOWLEDGE);
    self.flowLy.itemSize = CGSizeMake((self.frame.size.width - COLLECTWIDTH) / 3, (self.frame.size.width - COLLECTWIDTH) / 3 + COLLECTWIDTH);
    self.flowLy.minimumLineSpacing = COLLECTIONFOWLEDGE;
    self.flowLy.minimumInteritemSpacing = COLLECTIONFOWLEDGE;
    self.collectionV.frame = CGRectMake(0, TOPHEIGHT, self.frame.size.width, self.frame.size.height);
    
        self.titleLab.frame = CGRectMake(TITLELEFT, 0, TITLEWIDTH, TOPHEIGHT);
        self.moreBut.frame = CGRectMake(self.frame.size.width - MOVEWIDTH, self.titleLab.bounds.origin.y, MOVEWIDTH, TOPHEIGHT);
}
#pragma 头图片
- (void)addHeadView
{
    self.titleLab = [[UILabel alloc] init];
    self.titleLab.backgroundColor = [UIColor clearColor];

    self.titleLab.text = @"热门歌单";
    [self.contentView addSubview:self.titleLab];
    [_titleLab release];
    self.moreBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.moreBut.backgroundColor = [UIColor clearColor];
    [self.moreBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.moreBut setTitle:@"更多" forState:UIControlStateNormal];
    self.moreBut.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.moreBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self.contentView addSubview:self.moreBut];
    
    

}
#pragma 按钮的动作
- (void)buttonAction:(UIButton *)sender
{
    if (sender == self.moreBut) {
        NSLog(@"更多");
                NSString *str = SONGURL;
        [self.delegate changeHotSongVCUrl:str type:@"SONG"];
    }
}
#pragma 添加collectionView
- (void)addCollectionView
{
    self.flowLy = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLy];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionV];
    [_collectionV release];
    
    [self.collectionV registerClass:[LJM_HotSongs_CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}
#pragma collectionView的协议方法
#pragma 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
#pragma cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJM_HotSongs_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (0 != self.dataArr.count) {
       LJM_HotSong_Model *model = [self.dataArr objectAtIndex:indexPath.row];
        [cell.imageV sd_setImageWithURL:[NSURL  URLWithString:model.picUrl] placeholderImage:[UIImage imageNamed:@"zhanwei"]];
        cell.titleLab.text = model.name;
        cell.label.text = model.author;
        
    }

    return cell;
}
#pragma 点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJM_HotSong_Model *model = [self.dataArr objectAtIndex:indexPath.row];
    [self.delegate changeHotSongToSong_Root_sub:model];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
